package com.health.viewscontrols.medical.device.bg;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.health.R;
import com.vivachek.protocol.BaseResult;

import java.util.List;

public class ListDialogFragment extends DialogFragment {

    private OnItemSelectedListener listener;
    private List<BaseResult> items;

    public interface OnItemSelectedListener {
        void onItemSelected(BaseResult item);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ListView listView = view.findViewById(R.id.listView);
        CustomAdapter adapter = new CustomAdapter(getActivity(), items);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((parent, view1, position, id) -> {
            BaseResult selected = (BaseResult) parent.getItemAtPosition(position);
            if (listener != null) {
                listener.onItemSelected(selected);
            }
            dismiss();
        });
    }

    public void setItems(List<BaseResult> items) {
        this.items = items;
    }

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.listener = listener;
    }

    public static ListDialogFragment newInstance(List<BaseResult> items) {
        ListDialogFragment fragment = new ListDialogFragment();
        fragment.setItems(items);
        return fragment;
    }

    // 添加一个方法来更新数据
    public void updateData(List<BaseResult> newItems) {
        this.items = newItems;
        // 重新显示对话框
        show(getFragmentManager(), "list_dialog");
    }

    // 自定义适配器
    private class CustomAdapter extends ArrayAdapter<BaseResult> {

        private Context context;

        public CustomAdapter(Context context, List<BaseResult> items) {
            super(context, 0, items);
            this.context = context;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
            }

            BaseResult item = getItem(position);
            TextView textView = convertView.findViewById(android.R.id.text1);

            textView.setText(item.getTime() + " " + item.getSample().getDesc() + " " + item.getValue());

            return convertView;
        }
    }
}
